package main

import (
	"FenBuShiProject/mylog"
	"FenBuShiProject/portal"
	"FenBuShiProject/registry"
	"FenBuShiProject/service"
	"context"
	"fmt"
	"log"
)

func main() {
	if err := portal.ImportTemplates(); err != nil {
		log.Fatal(err)
	}
	host, port := "localhost", "5000"
	serviceAddress := fmt.Sprintf("http://%s:%s", host, port)
	//配置注册参数
	r := registry.Registration{
		ServiceName: registry.PortalService,
		ServiceURL:  serviceAddress,
		RequiredServices: []registry.ServiceName{
			registry.LogService,
			registry.GradingService,
		},
		ServiceUpdateURL: serviceAddress + registry.ServiceUpdateURL,
		HeartbeatURL:     serviceAddress + registry.HeartbeatURL,
	}
	//启动服务
	ctx, err := service.Start(context.Background(),
		host,
		port,
		r,
		portal.RegisterHandlers)
	if err != nil {
		log.Fatal(err)
	}
	//启动所有依赖
	for i := range r.RequiredServices {
		if providerURLs, err := registry.GetProviderURLs(r.RequiredServices[i]); err == nil {
			for j := range providerURLs {
				fmt.Printf("%s found at %s ", r.RequiredServices[i], providerURLs[j])
				mylog.SetClientLogger(providerURLs[j], r.ServiceName)
			}
		}
	}
	<-ctx.Done()
	fmt.Println("Shutting down portal.")
}
